package com.icode.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.icode.gulimall.product.entity.BrandEntity;
import com.icode.gulimall.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.icode.gulimall.product.entity.CategoryBrandRelationEntity;
import com.icode.gulimall.product.service.CategoryBrandRelationService;
import com.icode.common.utils.PageUtils;
import com.icode.common.utils.R;


/**
 * 品牌分类关联
 *
 * @author my_code
 * @email 1345743081@gmail.com
 * @date 2020-10-27 19:38:13
 * /product/categorybrandrelation/brands/list
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService relationService;

    /**
     * 获取品牌关联的分类
     */
    @GetMapping("/catelog/list")
    public R catelogList(@RequestParam("brandId") Long brandId) {
        List<CategoryBrandRelationEntity> data = relationService.list(
                new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId)
        );
        return R.ok().put("data", data);
    }

    /**
     * 获取分类关联的品牌
     */
    @GetMapping("/brands/list")
    public R RelationBrandList(@RequestParam(value = "catId", required = false) Long catId) {
        List<BrandVo> vos = null;
        if (catId != null) {
            List<BrandEntity> brands = relationService.getBrandsByCatId(catId);
            vos = brands.stream().map((item) -> {
                BrandVo brandVo = new BrandVo();
                brandVo.setBrandId(item.getBrandId());
                brandVo.setBrandName(item.getName());
                return brandVo;
            }).collect(Collectors.toList());
        }
        return R.ok().put("data", vos);


    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = relationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        CategoryBrandRelationEntity categoryBrandRelation = relationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        relationService.saveDatail(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        relationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        relationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
